Auto Ptr
   HOME

TheInfoList



OR:

auto_ptr is a class
template Template may refer to: Tools * Die (manufacturing), used to cut or shape material * Mold, in a molding process * Stencil, a pattern or overlay used in graphic arts (drawing, painting, etc.) and sewing to replicate letters, shapes or designs ...
that was available in previous versions of the
C++ standard library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the original ANSI C standard, it wa ...
(declared in the
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
), which provides some basic RAII features for C++ raw pointers. It has been replaced by the unique_ptr class. The auto_ptr template class describes an object that stores a pointer to a single allocated object that ensures that the object to which it points gets destroyed automatically when control leaves a scope. The
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
standard made auto_ptr deprecated, replacing it with the unique_ptr class template. auto_ptr was fully removed in
C++17 C++17 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++17 replaced the prior version of the C++ standard, called C++14, and was later replaced by C++20. History Before the C++ Standards Committee fixed a 3-year rel ...
. For shared ownership, the
shared_ptr In computer science, a smart pointer is an abstract data type that simulates a Pointer (computer programming), pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bu ...
template class can be used. shared_ptr was defined in
C++11 C++11 is a version of the ISO/IEC 14882 standard for the C++ programming language. C++11 replaced the prior version of the C++ standard, called C++03, and was later replaced by C++14. The name follows the tradition of naming language versions by ...
and is also available in the
Boost library Boost is a set of libraries for the C++ programming language that provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions, and unit testing. It conta ...
for use with previous C++ versions.


Declaration

The auto_ptr class is declared in
ISO/IEC 14882 C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, section 20.4.5 as: namespace std


Semantics

The auto_ptr has semantics of strict ownership, meaning that the auto_ptr instance is the sole entity responsible for the object's lifetime. If an auto_ptr is copied, the source loses the reference. For example: #include #include using namespace std; int main(int argc, char **argv) This code will print a
NULL Null may refer to: Science, technology, and mathematics Computing * Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value * Null character, the zero-valued ASCII character, also designated by , often use ...
address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=). The raw pointer i in the example should not be deleted, as it will be deleted by the auto_ptr that owns the reference. In fact, new int could be passed directly into x, eliminating the need for i. Notice that the object pointed by an auto_ptr is destroyed using operator delete; this means that you should only use auto_ptr for pointers obtained with operator new. This excludes pointers returned by malloc/calloc/realloc, and pointers to arrays (because arrays are allocated by operator new /a>/code> and must be deallocated by operator delete[]). Because of its copy semantics, auto_ptr may not be used in STL containers that may perform element copies in their operations.


See also

* Smart pointer *
Generic programming Generic programming is a style of computer programming in which algorithms are written in terms of types ''to-be-specified-later'' that are then ''instantiated'' when needed for specific types provided as parameters. This approach, pioneered b ...


References


External links


Using auto_ptr effectively


* Article
Using the auto_ptr Class Template to Facilitate Dynamic Memory Management
by
Danny Kalev Danny is a masculine given name. It is related to the male name Daniel. It may refer to: People * Danny Altmann, British immunologist * Danny Antonucci, Canadian animator, director, producer, and writer *Danny Baker (born 1957), English journal ...
* Article
Container of auto_ptr
by
Zeeshan Amjad Zeeshan (Zişan) or Zeshan (Zeşan) is a Turkish given name, derived from words "Zee" ''(possessor of,'' from Arabic ''ذو)'' and "Shan" ''(high status'' or ''splendor,'' from Arabic شأن'')'', sometimes simply translated as "princely" or " ...
* Article
Update on auto_ptr
by
Scott Meyers Scott Douglas Meyers (born April 9, 1959) is an American author and software consultant, specializing in the C++ computer programming language. He is known for his ''Effective C++'' book series. During his career, he was a frequent speaker at con ...

auto_ptr Class Template Reference from GNU libstdc++


{{C++ programming language Articles with example C++ code Articles with underscores in the title C++ Standard Library